package com.teacher.game.model;

import android.graphics.Color;
import android.graphics.Rect;
import com.teacher.ellio.Assets;
import com.teacher.game.framework.util.Painter;
import com.teacher.game.framework.util.RandomNumberGenerator;

/**
 * 移动障碍物 - 垂直移动的挑战性障碍物
 * 增加游戏的动态性和挑战性
 */
public class MovingBlock extends AbstractObstacle {
    
    private Rect collisionRect = new Rect();
    
    // 移动参数
    private float originalY;           // 原始Y坐标
    private float moveSpeed;           // 移动速度
    private float moveRange;           // 移动范围
    private int moveDirection = 1;     // 移动方向 (1=向下, -1=向上)
    
    // 移动类型
    public enum MovePattern {
        VERTICAL_BOUNCE,    // 上下弹跳
        SINE_WAVE,          // 正弦波移动
        CIRCULAR,           // 圆形轨迹
        RANDOM_JITTER       // 随机抖动
    }
    
    private MovePattern movePattern;
    private float moveTimer = 0;       // 移动计时器
    
    /**
     * 构造函数
     */
    public MovingBlock(float x, float y, int width, int height) {
        super(x, y, width, height, ObstacleType.MOVING);
        
        this.originalY = y;
        this.moveSpeed = RandomNumberGenerator.getRandIntBetween(50, 120); // 50-120像素/秒
        this.moveRange = RandomNumberGenerator.getRandIntBetween(60, 100); // 60-100像素移动范围
        
        // 随机选择移动模式
        MovePattern[] patterns = MovePattern.values();
        this.movePattern = patterns[RandomNumberGenerator.getRandInt(patterns.length)];
    }
    
    @Override
    public void update(float delta, int baseSpeed) {
        // 水平移动（与静态障碍物相同）
        x += baseSpeed * delta;
        
        // 垂直移动（根据移动模式）
        updateVerticalMovement(delta);
        
        // 检查是否需要重置
        if (x <= -width) {
            resetToRightSide();
        }
    }
    
    /**
     * 更新垂直移动
     */
    private void updateVerticalMovement(float delta) {
        moveTimer += delta;
        
        switch (movePattern) {
            case VERTICAL_BOUNCE:
                updateBounceMovement(delta);
                break;
                
            case SINE_WAVE:
                updateSineWaveMovement();
                break;
                
            case CIRCULAR:
                updateCircularMovement();
                break;
                
            case RANDOM_JITTER:
                updateRandomJitterMovement(delta);
                break;
        }
        
        // 限制移动范围
        float minY = Math.max(50, originalY - moveRange/2);
        float maxY = Math.min(400, originalY + moveRange/2);
        y = Math.max(minY, Math.min(maxY, y));
    }
    
    /**
     * 弹跳移动模式
     */
    private void updateBounceMovement(float delta) {
        y += moveDirection * moveSpeed * delta;
        
        // 检查边界反转
        if (y <= originalY - moveRange/2 || y >= originalY + moveRange/2) {
            moveDirection *= -1;
        }
    }
    
    /**
     * 正弦波移动模式
     */
    private void updateSineWaveMovement() {
        float amplitude = moveRange / 2;
        y = originalY + amplitude * (float)Math.sin(moveTimer * 2.0);
    }
    
    /**
     * 圆形轨迹移动模式
     */
    private void updateCircularMovement() {
        float radius = moveRange / 3;
        y = originalY + radius * (float)Math.sin(moveTimer * 1.5);
        // 可以添加X轴的微小圆形移动，但要小心不影响碰撞判定
    }
    
    /**
     * 随机抖动移动模式
     */
    private void updateRandomJitterMovement(float delta) {
        if (moveTimer > 0.2f) { // 每0.2秒改变一次方向
            moveDirection = RandomNumberGenerator.getRandInt(3) - 1; // -1, 0, 1
            moveTimer = 0;
        }
        y += moveDirection * moveSpeed * 0.3f * delta; // 较慢的抖动
    }
    
    @Override
    public void render(Painter g) {
        if (isOnScreen(800)) {
            // 绘制移动障碍物（使用不同颜色区分）
            g.drawImage(Assets.block, (int)x, (int)y);
            
            // 添加移动指示器
            renderMovementIndicator(g);
        }
    }
    
    /**
     * 渲染移动指示器
     */
    private void renderMovementIndicator(Painter g) {
        // 绘制移动轨迹预示线
        g.setColor(Color.argb(100, 255, 255, 0)); // 半透明黄色
        
        switch (movePattern) {
            case VERTICAL_BOUNCE:
                // 绘制上下箭头指示器
                int centerX = (int)(x + width/2);
                g.fillRect(centerX - 2, (int)(originalY - moveRange/2), 4, (int)moveRange);
                break;
                
            case SINE_WAVE:
                // 绘制波浪线指示器
                drawSineWaveIndicator(g);
                break;
                
            case CIRCULAR:
                // 绘制圆形指示器
                int radius = (int)(moveRange / 3);
                g.setColor(Color.argb(80, 255, 255, 0));
                g.drawOval((int)(x + width/2 - radius), (int)(originalY - radius), 
                          radius * 2, radius * 2);
                break;
                
            case RANDOM_JITTER:
                // 绘制随机指示器（闪烁效果）
                if ((int)(moveTimer * 10) % 2 == 0) {
                    g.setColor(Color.argb(120, 255, 0, 0));
                    g.fillOval((int)(x + width/2 - 3), (int)(y + height/2 - 3), 6, 6);
                }
                break;
        }
    }
    
    /**
     * 绘制正弦波指示器
     */
    private void drawSineWaveIndicator(Painter g) {
        int steps = 10;
        float stepX = width / (float)steps;
        
        for (int i = 0; i < steps; i++) {
            float waveX = x + i * stepX;
            float waveY = originalY + (moveRange/4) * (float)Math.sin((moveTimer + i * 0.3) * 2.0);
            g.fillOval((int)waveX, (int)waveY, 2, 2);
        }
    }
    
    @Override
    public void reset(float newX, float newY) {
        setPosition(newX, newY);
        this.originalY = newY;
        this.moveTimer = 0;
        this.moveDirection = 1;
        isPassed = false;
        isActive = true;
        
        // 随机重新选择移动模式
        MovePattern[] patterns = MovePattern.values();
        this.movePattern = patterns[RandomNumberGenerator.getRandInt(patterns.length)];
    }
    
    @Override
    public Rect getCollisionRect() {
        collisionRect.set(
            (int)x, (int)y, 
            (int)x + width, (int)y + height
        );
        return collisionRect;
    }
    
    @Override
    public boolean requiresJump() {
        // 移动障碍物的跳跃需求是动态的
        return y <= 360;
    }
    
    @Override
    public boolean requiresDuck() {
        // 移动障碍物的下蹲需求是动态的
        return y > 360;
    }
    
    @Override
    public int getDifficultyScore() {
        // 移动障碍物难度更高
        int baseScore = 3;
        
        // 根据移动模式调整难度
        switch (movePattern) {
            case VERTICAL_BOUNCE: return baseScore + 1;
            case SINE_WAVE: return baseScore + 2;
            case CIRCULAR: return baseScore + 2;
            case RANDOM_JITTER: return baseScore + 3; // 最难预测
            default: return baseScore;
        }
    }
    
    @Override
    public int getRewardScore() {
        return 25; // 移动障碍物奖励更高
    }
    
    /**
     * 重置到屏幕右侧
     */
    private void resetToRightSide() {
        x += 1000;
        
        // 随机设置新的原始高度
        originalY = RandomNumberGenerator.getRandIntBetween(200, 370);
        y = originalY;
        
        // 重新随机化参数
        moveSpeed = RandomNumberGenerator.getRandIntBetween(50, 120);
        moveRange = RandomNumberGenerator.getRandIntBetween(60, 100);
        moveTimer = 0;
        moveDirection = 1;
        
        // 重置状态
        isPassed = false;
        isActive = true;
    }
    
    /**
     * 获取移动模式（用于调试和统计）
     */
    public MovePattern getMovePattern() {
        return movePattern;
    }
    
    /**
     * 获取当前移动状态描述
     */
    public String getMovementStatus() {
        return String.format("%s: Y=%.1f, Speed=%.1f, Range=%.1f", 
                movePattern.name(), y - originalY, moveSpeed, moveRange);
    }
}